package net.csdn.business.common.feign.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import net.csdn.business.common.constant.CommonConstants;
import net.csdn.business.common.utils.BusinessUtil;
import net.csdn.business.common.utils.IpUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

/**
 * @author likun
 * @date 2022/12/29 10:15
 */
public class TraceFeignClientInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        // 上下文 traceId
        String traceId = MDC.get(CommonConstants.TRACE_ID);
        template.header(CommonConstants.TRACE_ID, StringUtils.isNotBlank(traceId) ? traceId : BusinessUtil.genTraceId());
        // 传递IP
        template.header(CommonConstants.TRACE_IP, IpUtil.getIp());
    }
}